package com.ztewatch.wearcontacts;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Groups;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.ztewatch.wearcontacts.loader.SortContactsAndGroupLoader;
import com.ztewatch.wearcontacts.presenter.LoadCursorDataPresenter;
import com.ztewatch.wearcontacts.view.ContactCursorAdapter;
import com.ztewatch.wearcontacts.view.ILoadCursorDataView;
import com.ztewatch.wearcontacts.view.ScrollGridView;

public class WearContactEditMainActivity extends Activity implements
		ILoadCursorDataView {
	// private ScrollGridView base_edit_gridview;
	public static final String EDIT_MAIN_ACTIVITY_FALG = "is_edit_main_activity_flag";
	// private List<HashMap<String, Object>> baseDataList = new
	// ArrayList<HashMap<String, Object>>();
	private ScrollGridView contact_edit_gridview;
	private ContactCursorAdapter contactCursorAdapter;
	private LoadCursorDataPresenter mLoadCursorDataPresenter;

	// private static final String SORT_KEY_ORDER = Contacts.PINNED
	// + " COLLATE NOCASE ASC";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wear_contact_edit_main);
		// initBaseGridView();
		initContactGridView();
		initContentObserver();
		mLoadCursorDataPresenter = new LoadCursorDataPresenter(this, this,
				contactCursorAdapter);
	}

	private void initContentObserver() {
		getContentResolver().registerContentObserver(Groups.CONTENT_URI, true,
				new GroupObserver(new Handler()));
	}

	private final class GroupObserver extends ContentObserver {

		public GroupObserver(Handler handler) {
			super(handler);
		}

		@Override
		public void onChange(boolean selfChange) {
			super.onChange(selfChange);
			mLoadCursorDataPresenter.restartLoader(0);
		}
	}

	private void initContactGridView() {
		boolean isVoiceCall = getIntent().getBooleanExtra(
				WearContactVoiceCallMainActivity.VOICE_CALL_FLAG, false);
		contact_edit_gridview = (ScrollGridView) findViewById(R.id.contact_edit_gridview);
		contactCursorAdapter = new ContactCursorAdapter(this, null);
		contactCursorAdapter.setEditActivityFlag(true);
		contactCursorAdapter.setVoiceCallFlag(isVoiceCall);
		contact_edit_gridview.setAdapter(contactCursorAdapter);
		contact_edit_gridview
				.setOnItemClickListener(new ContactItemClickListener());
	}

	// private void initBaseGridView() {
	// base_edit_gridview = (ScrollGridView)
	// findViewById(R.id.base_edit_gridview);
	// HashMap<String, Object> itemHashMap = new HashMap<String, Object>();
	// itemHashMap.put(BaseGridViewAdapter.ITEM_IMAGE,
	// R.drawable.zte_contact_picture_holo_light);
	// itemHashMap.put(BaseGridViewAdapter.ITEM_TEXT, getResources()
	// .getString(R.string.base_newcontact));
	// itemHashMap.put(BaseGridViewAdapter.IS_DISABLE_FALG, true);
	// baseDataList.add(itemHashMap);
	//
	// HashMap<String, Object> itemHashMap1 = new HashMap<String, Object>();
	// itemHashMap1.put(BaseGridViewAdapter.ITEM_IMAGE,
	// R.drawable.zte_contact_picture_holo_light);
	// itemHashMap1.put(BaseGridViewAdapter.ITEM_TEXT, getResources()
	// .getString(R.string.base_edit));
	// itemHashMap1.put(BaseGridViewAdapter.IS_DISABLE_FALG, true);
	// baseDataList.add(itemHashMap1);
	//
	// HashMap<String, Object> itemHashMap2 = new HashMap<String, Object>();
	// itemHashMap2.put(BaseGridViewAdapter.ITEM_IMAGE,
	// R.drawable.zte_contact_picture_holo_light);
	// itemHashMap2.put(BaseGridViewAdapter.ITEM_TEXT, getResources()
	// .getString(R.string.base_Sort));
	// itemHashMap2.put(BaseGridViewAdapter.IS_DISABLE_FALG, true);
	// baseDataList.add(itemHashMap2);
	//
	// HashMap<String, Object> itemHashMap3 = new HashMap<String, Object>();
	// itemHashMap3.put(BaseGridViewAdapter.ITEM_IMAGE,
	// R.drawable.zte_contact_picture_holo_light);
	// itemHashMap3.put(BaseGridViewAdapter.ITEM_TEXT, getResources()
	// .getString(R.string.base_Folder));
	// itemHashMap3.put(BaseGridViewAdapter.IS_DISABLE_FALG, true);
	// baseDataList.add(itemHashMap3);
	// BaseGridViewAdapter baseGridViewAdapter = new BaseGridViewAdapter(this,
	// baseDataList);
	// base_edit_gridview.setAdapter(baseGridViewAdapter);
	// base_edit_gridview.setEnabled(false);
	// }

	private class ContactItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if (!(boolean) (view.getTag(ContactCursorAdapter.IS_CLICK_FLAG))) {
				return;
			}
			if (position == 0) {// History
				Intent intent = new Intent();
				intent.setClass(WearContactEditMainActivity.this,
						WearContactCallLogActivity.class);
				startActivity(intent);
				return;
			}
			Cursor cursor = (Cursor) contactCursorAdapter.getItem(position);
			if (cursor != null) {
				int is_group_index = cursor
						.getColumnIndex(SortContactsAndGroupLoader.IS_GROUP);
				if (is_group_index != -1) {
					int is_group = cursor.getInt(is_group_index);
					if (is_group == 0) {// contact
						Uri contactUri = getContactUri(cursor);
						if (contactUri != null) {
							int starred_index = cursor
									.getColumnIndex(Contacts.STARRED);
							int starred_id = 0;
							if (starred_index != -1) {
								starred_id = cursor.getInt(starred_index);
							}
							Intent intent = new Intent();
							if (starred_id != 0
									&& getResources().getBoolean(
											R.bool.config_LGU_flag)) {
								intent.setAction(PasswordkeypadActivity.EDIT_CONTACT_ACTION);
								intent.putExtra(
										WearContactDisplayActivity.CONTACT_DETAIL_FALG,
										true);
								intent.setClass(
										WearContactEditMainActivity.this,
										PasswordkeypadActivity.class);
							} else {
								intent.setClass(
										WearContactEditMainActivity.this,
										WearContactEditActivity.class);
							}
							intent.putExtra(EDIT_MAIN_ACTIVITY_FALG, true);
							intent.setData(contactUri);
							startActivity(intent);
						}
					} else {// group
						int groupId_index = cursor.getColumnIndex(Groups._ID);
						if (groupId_index != -1) {
							long groupId = cursor.getLong(groupId_index);
							Uri group_uri = getGroupUriFromId(groupId);

							Intent intent = new Intent();
							intent.setData(group_uri);
							Bundle bundle = new Bundle();
							int title_index = cursor
									.getColumnIndex(Contacts.DISPLAY_NAME_PRIMARY);// Groups.TITLE
							if (title_index != -1) {
								String title = cursor.getString(title_index);
								bundle.putString(
										WearContactGroupListActivity.GROUP_TITLE,
										title);
							}
							// int count_index = cursor
							// .getColumnIndex(Groups.SUMMARY_COUNT);
							// if (count_index != -1) {
							// int group_contact_num = cursor
							// .getInt(count_index);
							// bundle.putInt(
							// WearContactGroupListActivity.GROUP_DETAIL_NUM,
							// group_contact_num);
							// }
							intent.putExtras(bundle);
							intent.setClass(WearContactEditMainActivity.this,
									WearContactEditGroupActivity.class);
							intent.putExtra(EDIT_MAIN_ACTIVITY_FALG, true);
							startActivity(intent);
						}
					}
				}
			}
		}

	}

	private Uri getGroupUriFromId(long groupId) {
		return ContentUris.withAppendedId(Groups.CONTENT_URI, groupId);
	}

	public Uri getContactUri(Cursor cursor) {
		Uri uri = null;
		int id_index = cursor.getColumnIndex(Contacts._ID);
		int lookup_key_index = cursor.getColumnIndex(Contacts.LOOKUP_KEY);
		if (id_index != -1 && lookup_key_index != -1) {
			long contactId = cursor.getLong(id_index);
			String lookupKey = cursor.getString(lookup_key_index);
			uri = Contacts.getLookupUri(contactId, lookupKey);
		}
		return uri;
	}

	@Override
	protected void onStart() {
		super.onStart();
		mLoadCursorDataPresenter.restartLoader(0);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mLoadCursorDataPresenter.destroyLoader(0);
	}

	@Override
	public void getLoadFinishCursor(Cursor c) {
		contactCursorAdapter.swapCursor(c);
	}
}
